package Tetris;

import java.awt.Choice;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Config {
	public static String rotate="Up", left="Left", right="Right", down="Down", pause="P";
	private static ArrayList<Choice> choices;
	public static void openconfig(JFrame frame){
		choices=new ArrayList<Choice>();
		final JFrame option = new JFrame("Option");
		option.setSize(400,300);
		option.setResizable(false);
		option.setLocationRelativeTo(frame);
		option.setLayout(null);
		Choice left = addChoise("Left", option, 30, 30);
		left.select(Config.left);
		Choice right = addChoise("Right", option, 150, 30);
		right.select(Config.right);
		Choice down = addChoise("Down", option, 30, 80);
		down.select(Config.down);
		Choice rotate = addChoise("Rotate", option, 150, 80);
		rotate.select(Config.rotate);
		Choice pause = addChoise("Pause", option, 30, 130);
		pause.select(Config.pause);
		JButton done = new JButton("Done");
		done.setBounds(150, 220, 100, 30);
		done.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				option.dispose();
				saveChanges();
			}
		});
		option.add(done);
		option.setVisible(true);
	}
	public static void saveChanges(){
		Choice left = choices.get(0);
		Choice right = choices.get(1);
		Choice rotate = choices.get(2);
		Choice down = choices.get(3);
		Choice pause = choices.get(4);
		Config.left = left.getSelectedItem();
		Config.right = right.getSelectedItem();
		Config.rotate = rotate.getSelectedItem();
		Config.down = down.getSelectedItem();
		Config.pause = pause.getSelectedItem();
		try{
			saveConfig();
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public static Choice addChoise(String name, JFrame option, int x, int y){
		JLabel lable = new JLabel(name);
		lable.setBounds(x, y-20, 100, 20);
		Choice key = new Choice();
		for(String s: getKeyName()){
			key.add(s);
		}
		key.setBounds(x, y, 100, 20);
		option.add(key);
		option.add(lable);
		choices.add(key);
		return key;
	}
	public static ArrayList<String> getKeyName(){
		ArrayList<String> result = new ArrayList<String>();
		for(String s: Keygether.keyNames){
			result.add(s);
			if(s.equalsIgnoreCase("F24")){
				break;
			}
		}
		return result;
	}
	
	public static void loadConfig() throws Exception{
		File directory= new File(getDefaultDirectory(),"/Tetris");
		if(!directory.exists()){
			directory.mkdir();
		}
		File config = new File(getDefaultDirectory(),"/config.txt");
		if(config.exists()){
			config.createNewFile();
			System.out.println("file not found, saving default");
			saveConfig();
			return;
		}
		Scanner s = new Scanner(config);
		HashMap<String, String> values = new HashMap<String, String>();
		while(s.hasNextLine()){
			String[] entry=s.nextLine().split(":");
			String key=entry[0];
			String value=entry[1];
			values.put(key, value);
			}
		//s.close();
		if(values.size() != 5){
			System.out.println("config is unnusable, saving default");
			saveConfig();
			return;
		}
		if(!values.containsKey("left")||!values.containsKey("right")||!values.containsKey("rotate")||!values.containsKey("down")||!values.containsKey("pause")){
			System.out.println("invalid names in config, saving default");
			saveConfig();
			return;
		}
		String left=values.get("left");
		String right=values.get("right");
		String rotate=values.get("rotate");
		String down=values.get("down");
		String pause=values.get("pause");
		
		if(!(getKeyName().contains(left)&&getKeyName().contains(right)&&getKeyName().contains(rotate)&&getKeyName().contains(down)&&getKeyName().contains(pause))){
			System.out.println("Invalid key in config, saving default");
			return;
		}
		Config.left=left;
		Config.right=right;
		Config.rotate=rotate;
		Config.down=down;
		Config.pause=pause;
	}
	
	public static void saveConfig() throws Exception{
		File config = new File(getDefaultDirectory(),"/Tetris/config.txt");
		if(config.exists()){
			config.createNewFile();
		}
		PrintWriter pw = new PrintWriter(config);
		pw.println("right:" + right);
		pw.println("left:" + left);
		pw.println("rotate:" + rotate);
		pw.println("down:" + down);
		pw.println("pause:" + pause);
		pw.close();
	}
	
	public static String getDefaultDirectory(){
		String OS = System.getProperty("os.name").toUpperCase();
		if(OS.contains("WIN")){
			return System.getenv("APPDATA");
		}
		if(OS.contains("MAC")){
			return System.getProperty("user.home")+"Library/Application  support";
		}
		return System.getProperty("uer.home");
	}
	
}
